//
//  SKShakeView.m
//  Gastrosoph
//
//  Created by 张明磊 on 12-10-30.
//
//

#import "SKShakeView.h"
#import "MBProgressHUD.h"
#import "SKHeaderFiles.h"

#define SK_IMAGEVIEW_UP_SHAKEVIEW CGRectMake(0, 54, 320, 76)

#define SK_IMAGEVIEW_DOWN_SHAKEVIEW CGRectMake(0, 130, 320, 228)

#define SK_IMAGEVIEW_MIDDLE_SHAKEVIEW CGRectMake(0, 82, 320, 97)

#define kCloseDuration   2.0f


@implementation SKShakeView
{
    BOOL        _expand;
}

@synthesize imageView_shake_up = _imageView_shake_up;

@synthesize imageView_shake_down = _imageView_shake_down;

@synthesize locationManager_shake = _locationManager_shake;

@synthesize theRegion_shake = _theRegion_shake;

@synthesize accelerManager = _accelerManager;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        [self install_view_shake];
        
        _accelerManager = [UIAccelerometer sharedAccelerometer];
        
        _accelerManager.delegate = self;
        
        _accelerManager.updateInterval = 1.0/60.0;
        
        _expand = NO;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)install_view_shake
{
    _imageView_shake_up = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageView_shake_up.png"]] autorelease];
    
    [_imageView_shake_up setFrame:SK_IMAGEVIEW_UP_SHAKEVIEW];
    
    _imageView_shake_down = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imageView_shake_down.png"]] autorelease];
    
    [_imageView_shake_down setFrame:SK_IMAGEVIEW_DOWN_SHAKEVIEW];
    
    UIImageView *imageView_shake_middle = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"imageView_shake_middle"]];
    
    [imageView_shake_middle setFrame:SK_IMAGEVIEW_MIDDLE_SHAKEVIEW];
    
    [self addSubview:imageView_shake_middle];
    
    [imageView_shake_middle release];
    
    [self addSubview:_imageView_shake_up];
    
    [self addSubview:_imageView_shake_down];
}


- (void)dealloc
{
    _locationManager_shake.delegate = nil;
    
    _accelerManager.delegate = nil;
    
    _accelerManager = nil;
   
    [_imageView_shake_up release], _imageView_shake_up = nil;
    
    [_imageView_shake_down release], _imageView_shake_down = nil;
    
    [_locationManager_shake release], _locationManager_shake = nil;
    
    [super dealloc];
}


#pragma mark - Accelerdelegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    if (_expand)
    {
        return;
    }
    
	static NSInteger shakeCount = 0;
    
	static NSDate *shakeStart;
	
	NSDate *now = [[NSDate alloc] init];
    
	NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];
	
	if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)
	{
		shakeCount = 0;
        
		[shakeStart release];
        
		shakeStart = [[NSDate alloc] init];
	}
	
	[now release];
    
	[checkDate release];
	
	if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)
	{
		shakeCount++;
		if (shakeCount > 0)
		{
            [self _expandAnimate];
		
			shakeCount = 0;
            
			[shakeStart release];
            
			shakeStart = [[NSDate alloc] init];
		}
	}
}

- (void)_expandAnimate
{ 
    [UIView beginAnimations:nil context:nil];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [UIView setAnimationDuration:0.25f];
    
    [UIView setAnimationRepeatCount:1];
    
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    [UIView setAnimationDelegate:self];
    
    _imageView_shake_up.center = CGPointMake(160, 51);
        
    _imageView_shake_down.center = CGPointMake(160, 285);
    
    [self view_showProgressHUD:@"正在加载信息,请稍后"];
    
    [UIView commitAnimations];
        
    [self performSelector:@selector(_closeAnimate) withObject:nil afterDelay:kCloseDuration];
   
    if(self.locationManager_shake)
    {
        [_locationManager_shake startUpdatingLocation];
        
        _locationManager_shake.desiredAccuracy = kCLLocationAccuracyBest;
        
        _locationManager_shake.distanceFilter = 500;
        
        MKCoordinateRegion theRegion;
        
        theRegion.center = [[self.locationManager_shake location] coordinate];    
    }
    
    else
    {
        self.locationManager_shake = [[[CLLocationManager alloc] init] autorelease];
    
        [_locationManager_shake startUpdatingLocation];
    
        _locationManager_shake.delegate = self;
    
        _locationManager_shake.desiredAccuracy = kCLLocationAccuracyBest;
    
        _locationManager_shake.distanceFilter = 500;
        
        _theRegion_shake.center = [[self.locationManager_shake location] coordinate];
    }
   
    _expand = !_expand;
}

-(void) _closeAnimate
{
    [UIView beginAnimations:nil context:nil];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [UIView setAnimationDuration:0.25f];
    
    [UIView setAnimationRepeatCount:1];
    
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    [UIView setAnimationDelegate:self];
    
    _imageView_shake_up.center = CGPointMake(160, 92);
    
    _imageView_shake_down.center = CGPointMake(160, 244);
    
    _expand = !_expand;
    
    [_locationManager_shake stopUpdatingLocation];
    
    NSLog(@"%f",_theRegion_shake.center.latitude);
    
    NSLog(@"%f",_theRegion_shake.center.longitude);
    
    [UIView commitAnimations];

}


#pragma mark -
#pragma mark MBProgressHUD


- (void)view_showProgressHUD:(NSString *) _infoContent
{
    MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:[self superview] animated:YES];
    
    [progressHUD setLabelText:_infoContent];
    
    [progressHUD setLabelFont:[UIFont fontWithName:SK_TEXTFONT size:13.0f]];
    
    [progressHUD setRemoveFromSuperViewOnHide:YES];
    
    [self performSelector:@selector(view_hideProgressHUD) withObject:nil afterDelay:2.0f];
}

- (void)view_hideProgressHUD
{
    [MBProgressHUD hideHUDForView:[self superview] animated:YES];
}




@end
